package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.StockService;
import com.ewili.x.special.task.thread.inter.StockThreadTask;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.StringUtils.startsWith;

@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private StockThreadTask stockThreadTask;

    @Autowired
    private StockDao stockDao;

    @Override
    public void spideStock() {
        String stockListUrl = "http://quote.eastmoney.com/stock_list.html";
        try {
            spide(stockListUrl);
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        } catch (RuntimeException e) {
            ExceptionUtils.propagate(e);
        }
    }

    @Override
    public String getCodeFromUrl(String url, String start, String end, boolean clearMarket) {
        String temp = StringUtils.remove(url, start);
        temp = StringUtils.remove(temp, end);
        if (clearMarket) {
            temp = StringUtils.replace(temp, "sz|sh", "");
        }
        return temp;
    }

    private void spide(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("a");
        for (Element link : links) {
            String linkHref = link.attr("href");
            if (StringUtils.contains(linkHref, "sz00") || StringUtils.contains(linkHref, "sz30") || StringUtils.contains(linkHref, "sh60")) {
                String code = getCode(linkHref);
                this.saveData(code);
            }
        }
    }

    private void saveData(String code) {
        String pattern = "(\\D*)(\\d+)(.*)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(code);
        if (m.find()) {
            Stock stock = stockDao.findByCode(m.group(2));
            if (stock == null) {
                stock = new Stock();
                stock.setCode(m.group(2));
                String market = startsWith(m.group(2), "60") ? "sh" : "sz";
                stock.setMarket(market);
                try {
                    stock.setLastHisRecordDate(DateUtils.parseDate("19910101", DateUtils.yyyyMMdd));
                    stock.setLastAdjCloseDate(DateUtils.parseDate("19910101", DateUtils.yyyyMMdd));
                    stock.setLastFinanceReportDate(DateUtils.parseDate("19910101", DateUtils.yyyyMMdd));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                stockDao.save(stock);
            }
        }
    }

    private String getCode(String url) {
         return StringUtils.substring(url,url.length()-13, url.length()-5);
    }

}
